﻿using System;
using BeefyEventAggregator;
using MATtutsLab.Entities;
using MATtutsLab.MVP.Events;
using MATtutsLab.MVP.Views;
using MATtutsLab.Repositories;

namespace MATtutsLab.MVP.Presenters
{
    public class AddEditZombieMoviePresenter
    {
        protected IAddEditZombieMovieView View;
        protected IMovieRepository Repository;
        protected IEventPublisher EventPublisher;
        protected Movie Movie;

        public AddEditZombieMoviePresenter(IAddEditZombieMovieView view, IMovieRepository repository, IEventPublisher eventPublisher)
        {
            View = view;
            View.Presenter = this;

            Repository = repository;
            EventPublisher = eventPublisher;
        }

        public void InitializeView()
        {
            if (Movie == null)
                Movie = new Movie();

            View.DisableSave();
            View.SetTitleToAddMovie();
        }

        public void InitializeView(Movie movieToEdit)
        {
            if (movieToEdit == null)
                throw new NullReferenceException("MovieToEdit cannot be null");

            Movie = movieToEdit;

            View.SetTitleToEditMovie();
            View.LoadFormValuesFromExistingMovie(Movie);
        }


        public void TitleEntered(string title)
        {
            Movie.Title = title;

            CheckValidity();
        }

        private void CheckValidity()
        {
            var isValid = true;

            isValid &= ValidateTitle(Movie.Title);
            isValid &= ValidateShortDescription(Movie.ShortDescription);
            isValid &= ValidateLongDescription(Movie.LongDescription);
            isValid &= ValidateImageUrl(Movie.Image);

            if (isValid)
                View.EnableSave();
            else
                View.DisableSave();
        }

        private bool ValidateImageUrl(string imageUrl)
        {
            var isValid = true;

            if (string.IsNullOrWhiteSpace(imageUrl))
            {
                View.ShowImageUrlIsInvalidMessage();

                isValid = false;
            }
            else
                View.HideImageUrlIsInvalidMessage();


            return isValid;
        }

        private bool ValidateLongDescription(string longDescription)
        {
            var isValid = true;

            if (string.IsNullOrWhiteSpace(longDescription))
            {
                View.ShowLongDescriptionIsInvalidMessage();

                isValid = false;
            }
            else
                View.HideLongDescriptionIsInvalidMessage();


            return isValid;
        }

        private bool ValidateShortDescription(string shortDescription)
        {
            var isValid = true;

            if (string.IsNullOrWhiteSpace(shortDescription))
            {
                View.ShowShortDescriptionIsInvalidMessage();

                isValid = false;
            }
            else
                View.HideShortDescriptionIsInvalidMessage();
            
            
            return isValid;
        }

        private bool ValidateTitle(string title)
        {
            var isValid = true;

            if (string.IsNullOrWhiteSpace(title))
            {
                View.ShowTitleIsInvalidMessage();

                isValid = false;
            }
            else
                View.HideTitleIsInvalidMessage();


            return isValid;
        }

        public void ShortDescriptionEntered(string shortdescription)
        {
            Movie.ShortDescription = shortdescription;

            CheckValidity();
        }

        public void LongDescriptionEntered(string longDescription)
        {
            Movie.LongDescription = longDescription;

            CheckValidity();
        }

        public void ImageUrlEntered(string imageUrl)
        {
            Movie.Image = imageUrl;

            CheckValidity();
        }

        public void SaveRequested()
        {
            Repository.Save(Movie);

            EventPublisher.Publish(new MovieUpdatedEvent(Movie));

            View.CloseRequested();
        }
    }
}
